package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexSearchVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@Slf4j
@SuppressWarnings({"unchecked", "rawtypes"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    ///api/search/albumInfo/upperAlbum/{albumId}
    @Operation(summary = "专辑上架接口")
    @GetMapping(value = "upperAlbum/{albumId}")
    public Result upperAlbum(@PathVariable(name = "albumId") Long albumId){
        searchService.upperAlbum(albumId);
        return Result.ok();
    }

   // /api/search/albumInfo/lowerAlbum/{albumId}
    @Operation(summary = "专辑下架接口")
    @GetMapping(value = "lowerAlbum/{albumId}")
    public Result lowerAlbum(@PathVariable(name = "albumId") Long albumId){
        searchService.lowerAlbum(albumId);
        return Result.ok();
    }

    @Operation(summary = "批量上架")
    @GetMapping("/batchUpperAblum")
    public Result upperAlbum(){
        for (long i = 2; i <= 1500; i++) {   // 专辑id为1数据补全，缺少专辑属性数据
            searchService.upperAlbum(i);
        }
        return Result.ok();
    }

    @Operation(summary = "搜索")
    @PostMapping
    public Result<AlbumSearchResponseVo> srarch(@RequestBody AlbumIndexQuery albumIndexQuery){
        AlbumSearchResponseVo albumSearchResponseVo = searchService.search(albumIndexQuery);
        return Result.ok(albumSearchResponseVo);
    }


    // /api/search/albumInfo/channel/{category1Id}
    @SneakyThrows
    @Operation(summary = "获取频道页数据")
    @GetMapping(value = "/channel/{category1Id}")
    public Result<List<AlbumInfoIndexSearchVo>> channel(@PathVariable(value = "category1Id") Long category1Id) {
        List<AlbumInfoIndexSearchVo> albumInfoIndexSearchVos =  searchService.channel(category1Id) ;
        return Result.ok(albumInfoIndexSearchVos) ;
    }

    @SneakyThrows  // 将当前方法抛出的异常，包装成RuntimeException，骗过编译器，使得调用点可以不用显示处理异常信息。
    @Operation(summary = "更新排行榜")
    @GetMapping("/updateLatelyAlbumRanking")
    public Result updateLatelyAlbumRanking() {
        searchService.updateLatelyAlbumRanking();
        log.info("更新排行榜功能成功了....");
        return Result.ok();
    }

    @Operation(summary = "获取排行榜列表")
    @Parameters({
            @Parameter(name = "category1Id",description = "一级分类",in = ParameterIn.PATH,required = true),
            @Parameter(name = "dimension",description = "热度:hotScore、播放量:playStatNum、订阅量:subscribeStatNum、购买量:buyStatNum、评论数:albumCommentStatNum",required = true,in = ParameterIn.PATH),
    })
    @GetMapping("findRankingList/{category1Id}/{dimension}")
    public Result<List<AlbumInfoIndex>> findRankingList(@PathVariable Long category1Id,
                                                        @PathVariable String dimension){
        List<AlbumInfoIndex> infoIndexVoList = searchService.findRankingList(category1Id, dimension);
        return Result.ok(infoIndexVoList);

    }

}

